//package download;
//
//import util.FileUtils;
//import util.Md5Utils;
//import util.ProgressUtil;
//
//import java.io.File;
//import java.io.IOException;
//import java.security.NoSuchAlgorithmException;
//import java.util.ArrayList;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.atomic.AtomicLong;
//
///**
// * @Description  管理多任务
// * @Author 冷彭磊
// * @Data 2025/2/26 9:09
// */
//public class DownloadManager {
//    //文件url
//    private final ArrayList<String> urlList;
//    //存储位置
//    private final ArrayList<String> pathList;
//    private  final ArrayList<String> md5List ;
//    private DownloadListener listener;
//    //线程数量
//    private int threadNum;
//    public DownloadManager(ArrayList<String> urlList,ArrayList<String> pathList,ArrayList<String> md5List,int threadNum,DownloadListener listener){
//        this.urlList = urlList;
//        this.pathList = pathList;
//        this.md5List = md5List;
//        this.threadNum  = threadNum;
//        this.listener  = listener;
//    }
//
//    public void startDownload() throws IOException, InterruptedException, NoSuchAlgorithmException {
//        System.out.println("----------------------------------------");
//        for (int i = 0; i < urlList.size(); i++) {
//            String url = urlList.get(i);
//            String path = pathList.get(i);
//            String md5 = md5List.get(i);
//
//            System.out.println("任务" + (i+1) + "开始下载");
//
//            //获取工具类
//             FileUtils fileUtils = new FileUtils();
//
//            //获取文件大小
//            final long fileSize = fileUtils.getFileLength(urlList.get(i));
//            if (fileSize <= 0){
//                listener.onError(url,"获取文件大小失败");
//                continue;
//            }
//
//            //检查文件是否支持断点续传
//            final boolean breakpointContinuation = fileUtils.isBreakpointContinuation(url);
//            listener.onBreakpointContinuation(url ,breakpointContinuation);
//
//
//            ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
//            AtomicLong totalDownloadedBytes = new AtomicLong(0);
//
//            //计算每个线程应该传递的数据量
//            long dataSize ;
//            dataSize = fileSize/ threadNum;
//            for (int i1 = 0; i1 < threadNum; i1++) {
//                long start = i1 * dataSize;
//                long end = (i1 == threadNum - 1) ? fileSize - 1 : start + dataSize - 1;
//                executorService.execute(new DownloadTask(start,end,url,path,totalDownloadedBytes,listener,fileSize));
//            }
//            executorService.shutdown(); // 关闭线程池，不再接受新任务
//            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
//            //检查数据完整性
//            if (totalDownloadedBytes.get() == fileSize){
//                //进行MD5校验
//                final String calculateMD5 = Md5Utils.calculateMd5(new File(path));
//                if (calculateMD5.equals(md5)){
////                    listener.onComplete();
//                    new ProgressUtil().deleteProgressFile(url);
//                }else{
//                    System.out.println("原始MD5:" + md5);
//                    System.out.println("本地MD5" + calculateMD5);
//                    listener.onError(url,"MD5 校验失败");
//                }
//            }else{
//                listener.onError(url,"下载不完整");
//                new File(path).delete();
//            }
//            totalDownloadedBytes.set(0);
//        }
//    }
//}
//
//
//
//
